package com.cenwangkeji.resident.ui.activity;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.cenwangkeji.resident.R;
import com.cenwangkeji.resident.model.Constant;
import com.cenwangkeji.resident.model.OkHttpUtils;
import com.cenwangkeji.resident.model.bean.AssessBean;
import com.cenwangkeji.resident.model.bean.User;
import com.cenwangkeji.resident.ui.adapter.AssessAdapter;
import com.google.gson.Gson;
import com.lws.basic.base.BaseActivity;
import com.lws.basic.persenter.ObserverOnNext;
import com.lws.basic.utils.common.MyLogger;
import com.lws.basic.utils.common.MyToast;
import com.lws.basic.utils.common.RxUtils;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;

//评估报告界面
public class AssessActivity extends BaseActivity {


    @BindView(R.id.rv)
    RecyclerView mRv;
    private AssessAdapter mAdapter;

    @Override
    public int setLayoutId() {
        return R.layout.activity_assess;
    }

    @Override
    public void initData() {
        super.initData();
        ButterKnife.bind(this);
        showLoading();
        initRecycelrView();
        getData();
    }

    private void getData(){
        Observable.create(new ObservableOnSubscribe<AssessBean>() {
            @Override
            public void subscribe(ObservableEmitter<AssessBean> emitter) throws Exception {
                Map<Object,Object> map = new HashMap<>();
                map.put( Constant.token, User.getToken());
                map.put( "userId",User.getUserId());
                map.put( "mark",3);
                JSONObject jsonObject = new JSONObject(map);
                MyLogger.i(jsonObject.toString());
                String json = OkHttpUtils.post(jsonObject.toString(), Constant.assess_list);
                MyLogger.i(json);
                Gson gson = new Gson();
                emitter.onNext(gson.fromJson(json, AssessBean.class ));
            }
        }).compose(RxUtils.<AssessBean>io_main()).subscribe(new ObserverOnNext<AssessBean>() {
            @Override
            public void onNext(AssessBean assessBean) {
                if (assessBean.getMsg().equals(Constant.token_expired)){
                    signIn();
                }else {
                    if (assessBean.getData().size()>0){
                        mAdapter.setNewData(assessBean.getData());
                    }else {
                        View emptyView = getLayoutInflater().inflate(R.layout.view_empty, null);
                        mAdapter.setEmptyView(emptyView);
                        MyLogger.i(assessBean.getMsg());
                        MyToast.show("没有评估");
                    }
                }
                dismissLoading();
            }
        });
    }

    private void initRecycelrView(){
        mRv.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new AssessAdapter(null);
        mRv.setAdapter(mAdapter);

    }

}
